package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.ItemDTO;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Override
    public PageDTO<Item> getAll(Integer page, Integer size) {
        Page<Item> page1 = new Page<>(page,size);
        itemMapper.selectPage(page1,null);
       /* List<Item> records = page1.getRecords();
        List<ItemDTO> list = records.stream().map(item -> {
            ItemDTO dto = new ItemDTO();
            BeanUtils.copyProperties(item, dto);
            return dto;
        }).collect(Collectors.toList());*/
        return new PageDTO<Item>(page1.getTotal(),page1.getRecords());
    }

    @Override
    public Item getId(Integer id) {
        return  itemMapper.selectById(id);
    }

    @Override
    public void insert(Item item) {
        itemMapper.insert(item);
    }

    @Override
    public void delete(Integer id) {
        itemMapper.deleteById(id);
    }
}
